<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: huajie <banhuajie@163.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Admin\Model\AuthGroupModel;
use Think\Page;


/**
 * 后台静态页控制器
 * @author huajie <banhuajie@163.com>
 */
class MakeHtmlController extends AdminController {

    public function build()
    {
        $this->meta_title = "静态页生成";
        $this->display();
    }

    public function save()
    {
        $type    =   I('post.type',0);
        $res = $this->getDir($type);
        if($res){
            $this->success('生成静态页成功',U('build'));
        } else {
            $this->error('生成静态页失败，请联系技术人员',U('build'));
        }
    }

    /**
     * 返回保存的文件夹
     * @param $type
     * @return string
     */
    public function getDir($type)
    {
        $dir = ROOT_PATH.C('HTML_TEMP');
        $name = '';
        $url = $_SERVER['HTTP_ORIGIN'];
        switch ($type){
            case 1: //游戏中心
                $url  .= "/index.php?s=/Media/Game/game_list.html";
                $name = "games_list.html";
                $dir .= "games/";
                //查询游戏的相关信息
                $game_list = D('Game')->where('game_status = 1')->field('id,game_name,game_name_a,game_type_id,game_type_name,icon,sibling_id,version,game_size,create_time,features,introduction,screenshot,and_dow_address,ios_dow_address,add_game_address,ios_game_address')->order('sort asc')->select();
                foreach($game_list as $k => $v){
                    $arr = [];
                    if(!empty($v['screenshot'])){
                        $pictureArr = explode(',',$v['screenshot']);
                        foreach($pictureArr as $kk => $vv){
                            $result = M('picture')->where('id',$vv)->field('id,path')->find();
                            $arr[] = $result['path'];
                        }

                    }
                    //游戏截图
                    $game_list[$k]['screenshot'] = $arr;
                    //获取图像icon
                    $game_list[$k]['icon'] = get_cover($v['icon'],'path');
                    //更新时间
                    $game_list[$k]['create_time'] = date('Y-m-d',$v['create_time']);
                    //游戏简介
                    $game_list[$k]['introduction'] = msubstr($v['introduction'],0,25);
                    $game_list[$k]['game_name'] = preg_replace('/\(.*?\)/', '', $v['game_name']);
                }
                $game_list = json_encode($game_list);
                $result = put_content_to_txt($dir,'game.json',$game_list);
                if($result == false){
                    $this->error('获取游戏相关信息失败',U('build'));
                }
                break;
            case 2: //礼包中心
                $url  .= "/index.php?s=/Media/Game/gift_list.html";
                $name = "gifts_list.html";
                $dir .="gifts/";
                //查询礼包信息
                $gift_list = D('Giftbag')->join('tab_game on tab_giftbag.game_id = tab_game.id')->where('tab_giftbag.status = 1')->field('tab_giftbag.id,giftbag_name,tab_giftbag.game_name,desribe,start_time,end_time,icon,game_id')->order('id desc')->select();
                foreach($gift_list as $k => $v){
                    //获取图像icon
                    $gift_list[$k]['icon'] = get_cover($v['icon'],'path');
                    $gift_list[$k]['game_name'] = preg_replace('/\(.*?\)/', '', $v['game_name']);
                    //剩余有效期
                    if($v['end_time']-time() > 0){
                        $gift_list[$k]['remaining_time'] = ceil(($v['end_time']-time())/86400);
                    }else{
                        $gift_list[$k]['remaining_time'] = 0;
                    }

                }
                $gift_list = json_encode($gift_list);
                $result = put_content_to_txt($dir,'gift.json',$gift_list);
                if($result == false){
                    $this->error('获取礼包相关信息失败',U('build'));
                }
                break;
            case 3: //新闻中心
                $url  .= "/index.php?s=/Media/Article/index/category/media.html";
                $name = "news_list.html";
                $dir .="news/";
                break;
            case 4: //客服中心
                $url  .= "/index.php?s=/Media/Service/index.html";
                $name .="services.html";
                $dir .="services/";
                break;
            default:
                //获取内容
                $url .="?type=1";
                $name = 'index.html';
                $dir  = ROOT_PATH."/";
                break;
        }
        $res = make_html($dir,$name,$url,$type);
        return  $res;

    }

}